<template>
	<div>
		<!--面包屑-->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>角色管理</el-breadcrumb-item>
			<el-breadcrumb-item>用户列表</el-breadcrumb-item>
		</el-breadcrumb>

		<!--内容卡片-->
		<el-card class="box-card">
			<!--搜索框-->
			<el-row :gutter="20">
				<el-col :span="5">
					<!--模糊搜索框-->
					<el-input placeholder="请输入查询用户名称" clearable="" @clear="select" v-model="queryUser.name"
						class="input-with-select">
						<el-button slot="append" icon="el-icon-search" @click="select"></el-button>
					</el-input>
				</el-col>
			</el-row>


			<!-- 表格数据  :data代表表数据  border：带有四边框-->
			<el-table :data="users" border style="width: 100%;">
				<!-- prop：表格当前列的字段名称-->
				<el-table-column type="index" label=""></el-table-column>
				<el-table-column prop="username" align="center" label="用户名"></el-table-column>
				<el-table-column prop="name" align="center" label="名称"></el-table-column>
				<el-table-column label="头像" align="center" width="160">
					<!--作用域插槽-->
					<template slot-scope="scope">
						<a @click="selectImg(scope.row.img)"><el-image style="width: 75px; height: 75px" :src="url+scope.row.img">
						</el-image></a>
						<!--图片放大后的对话框-->
						<el-dialog :visible.sync="dialogVisible">
							<img  width="50%" height="50%" :src="dialogImageUrl" alt="">
						</el-dialog>
					</template>
				</el-table-column>
				<el-table-column prop="sex" align="center" label="性别"></el-table-column>
				<el-table-column prop="age" align="center" label="年龄"></el-table-column>
				<el-table-column prop="phone" align="center" label="电话"></el-table-column>
				<el-table-column prop="xueli" align="center" label="学历"></el-table-column>
				<el-table-column prop="isyingjie" align="center" label="是否毕业"></el-table-column>
				<el-table-column prop="address" align="center" label="地址"></el-table-column>
				<el-table-column label="操作"  width="130" align="center">
					<template slot-scope="scope">
						<el-tooltip class="item" :enterable="false" effect="dark" content="设置为管理员" placement="top">
							<el-button type="warning" @click="UpdateUser(scope.row.id)" icon="el-icon-edit" size="mini">
							</el-button>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>

			<!--分页
					@size-change:当每页显示的条数改变时触发函数
					@current-change：当前页码的改变触发的函数
					current-page：当前页码
					page-sizes：每页显示的条数的数组
					page-size：默认每页显示的条数
					-->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryUser.pageNum" :page-sizes="[1,3,5,10]" :page-size="queryUser.pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="total" style="padding:25px 350px;">
			</el-pagination>
		</el-card>





	</div>
</template>

<script>
	import qs from 'qs'
	export default {
		data() {
			return {
				//模糊分页查询对象
				queryUser: {
					name: '',
					roleId:1,
					pageNum: 1,
					pageSize: 5
				},
				//权限列表集合
				users: [],
				//总数居条数
				total: 0,
				url:'http://localhost/showImg?filename=',
				//放大框
				dialogVisible:false,
				//图片预览的路径
				dialogImageUrl: ''
			}
		},
		methods: {
			//模糊分页查询
			async getUsers() {
				this.users=[];
				var res = await this.$http.post("userSelectAll", qs.stringify(this.queryUser))
				if (res.data.code == 200) {
					this.$message.success(res.data.message)
					//查询数据赋值给powers
					this.users = res.data.data.records;
					//获取数据的总条数
					this.total = res.data.data.total;
				} else {
					this.$message.error(res.data.message)
				}
			},
			//改变每页显示的条数
			handleSizeChange(newpageSize) {
				this.queryUser.pageSize = newpageSize;
				this.getUsers();
			},
			//当前页码改变时触发的函数
			handleCurrentChange(newpageNum) {
				this.queryUser.pageNum = newpageNum;
				this.getUsers();
			},
			//根据权限名模糊查询
			select() {
				this.queryUser.pageNum = 1;
				this.getUsers();
			},
			//修改用户角色
			UpdateUser(id) {
				this.$confirm('该用户设置为管理员, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					var res = await this.$http.get("UpdateUserRole?id=" + id)
					if (res.data.code == 200) {
						this.$message.success(res.data.message);
					} else {
						this.$message.error(res.data.message);
					}
					this.getUsers();
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消操作'
					});
				});
			},
			//图片展示
			selectImg(img){
				this.dialogImageUrl = this.url+img;
				this.dialogVisible = true;
			}
		
		},
		//当前页码加载前执行的代码
		mounted() {
			this.getUsers()
		}
	}
</script>

<style scoped>
	.el-breadcrumb {
		font-size: 16px !important;
		margin: 15px 8px;
		margin-bottom: 20px;
	}
	/deep/.el-table__body-wrapper{
		color:coral;
	}
</style>
